package com.kq.tallybookl.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.kq.tallybookl.R;
import com.kq.tallybookl.item.MenoItem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 数据加载适配器类
 * Created by Kq on 2016/4/30.
 */
public class ItemAdapter extends BaseAdapter{

    private ArrayList<MenoItem> list;
    private Context activity;
    private boolean selectStatus;
    private boolean lastStatus;
    private LayoutInflater inflater;

    public ItemAdapter(ArrayList<MenoItem> list, Context activity) {
        this.list = list;
        this.activity = activity;
        selectStatus = false;
        lastStatus = selectStatus;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MenoItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // 初始化控件
        ViewHolder holder = null;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listview_item,null);
            holder.title = (TextView) convertView.findViewById(R.id.listView_item_textView_title);
            holder.time = (TextView) convertView.findViewById(R.id.listView_item_textView_time);
            holder.select = (Button)convertView.findViewById(R.id.listView_item_button_select);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // 设置信息
        holder.title.setText(list.get(position).getTitle());
        SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");
        Date date = new Date(list.get(position).getTimestamp());
        holder.time.setText(format.format(date));
        // 设置按钮选择状态
        if(selectStatus){
            if(lastStatus!=selectStatus){
                // 记录上次加载状态
                lastStatus = selectStatus;
                // 设置动画
                TranslateAnimation slideLeft = new TranslateAnimation(0,100,0,0);
                TranslateAnimation slideRight = new TranslateAnimation(100,0,0,0);
                slideLeft.setInterpolator(new AccelerateInterpolator());
                slideRight.setInterpolator(new AccelerateInterpolator());
                // 设置动画时间
                slideLeft.setDuration(500);
                slideRight.setDuration(500);
                final View viewLeft = holder.time;
                final View viewRight = holder.select;
                // 设置动画前后按钮显示状态
                slideLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        viewLeft.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        viewLeft.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                slideRight.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        viewRight.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                // 启动动画
                viewLeft.startAnimation(slideLeft);
                viewRight.startAnimation(slideRight);
            }else {
                holder.time.setVisibility(View.GONE);
                holder.select.setVisibility(View.VISIBLE);
            }
            if(list.get(position).isStatus()){
                holder.select.setBackgroundColor(activity.getResources().getColor(R.color.colorButtonSelect));
            }else{
                holder.select.setBackgroundColor(activity.getResources().getColor(R.color.colorButtonUnSelect));
            }
        }else {
            if(lastStatus!=selectStatus){
                lastStatus = selectStatus;
                TranslateAnimation slideLeft = new TranslateAnimation(0,100,0,0);
                TranslateAnimation slideRight = new TranslateAnimation(100,0,0,0);
                slideLeft.setInterpolator(new AccelerateInterpolator());
                slideRight.setInterpolator(new AccelerateInterpolator());
                slideLeft.setDuration(500);
                slideRight.setDuration(500);
                final View viewLeft = holder.select;
                final View viewRight = holder.time;
                slideLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        viewLeft.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        viewLeft.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                slideRight.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        viewRight.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                viewLeft.startAnimation(slideLeft);
                viewRight.startAnimation(slideRight);
            }else {
                holder.time.setVisibility(View.VISIBLE);
                holder.select.setVisibility(View.GONE);
            }
            holder.select.setBackgroundColor(activity.getResources().getColor(R.color.colorButtonUnSelect));
        }

        return convertView;
    }

    class ViewHolder{
        TextView title;
        TextView time;
        Button select;
    }

    public void changeStatus(boolean status){
        selectStatus = status;
    }

    public boolean isSelectStatus() {
        return selectStatus;
    }
}
